<?php
/**
 * @file $HeadURL: eventclass.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-3-19 8:08:45 $
 * @brief 
 */

namespace haibao\cms\business;
use \haibao\cms\data\EventCategoryDefine as CategoryDefineData;
use \haibao\frame\data\query\Condition ;

class EventCategoryDefine extends \haibao\cms\business\Base{
	
	public static function addEventClass($model){
		self::checkOperation(self::FUNC_CODE_EVENT_CATEGORY_ADD);
		if(!isset($model->ParentId)){
			throw new BusinessException('请选择父级分类');
		}	
		if(!$model->CategoryName){
			throw new BusinessException('分类名不能为空');
		}		
		if(!self::hasSameCategoryName($model->CategoryName,$model->Alias,$model->ParentId,'')){
			throw new BusinessException('分类名称或别名已存在',Base::DATA_ERROR_NAME_EXIST);
		}		
		$eventClassModel = new CategoryDefineData();
		$eventClassModel->addEventClass($model);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_CATEGORY_ADD, $model->toArray(false));
	}
	
	public static function updateEventClass($model){
		self::checkOperation(self::FUNC_CODE_EVENT_CATEGORY_SAVE);
		
		if(!$model->ParentId && $model->ParentId!=0 ){
			throw new BusinessException('分类Id不能为空');
		}
		if(!$model->CategoryName ){
			throw new BusinessException('分类Id不能为空');
		}
		if(!self::hasSameCategoryName($model->CategoryName,$model->Alias,$model->ParentId,$model->Id)){
			throw new BusinessException('分类名称或别名已存在',Base::DATA_ERROR_NAME_EXIST);
		}
		$eventClassModel = new CategoryDefineData();
		$eventClassModel->updateEventClass($model);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_CATEGORY_SAVE, $model->toArray(false));
	}
	
	public static function getEventClassArr($parentId=0){
		$eventClassModel = new CategoryDefineData();
		return $eventClassModel->getEventClassArr($parentId);
	}
	
	public static function showEventClass(){	
		$eventClassModel = new CategoryDefineData();
		return $eventClassModel->getCategoryTree();
	}
	
	public static function getAllCategorys(){
		$eventClassModel = new CategoryDefineData();
		return $eventClassModel->getAllCategorysDict();
	}
	
	public static function getCategoryById($id){
		if(!$id){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();
		$model = $data->getCategoryById($id);
		if (!$model){
			throw new BusinessException('分类不存在', self::DATA_EMPTY);
		}
	
		return $model;
	}
	
	public static function getSmallEventClass($parentId){
		if(!$parentId){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();
		$model = $data->getCategoryById($parentId);
	}
	
	public static function deleteEventCategoryDefine($categoryId){
		self::checkOperation(self::FUNC_CODE_EVENT_CATEGORY_DELETE);
		if(!$categoryId){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();				
		$data->deleteEventClass($categoryId);
		
		self::saveOperateHistory(self::FUNC_CODE_EVENT_CATEGORY_DELETE, array('Id' =>$categoryId));
	}
	
	public static function getAllChildrenCategory($categoryId){
		if(!$categoryId){
			throw new BusinessException('分类id不能为空');
		}
		$data = new CategoryDefineData();
		return $data->getAllChildrenCategory($categoryId);
	}
	
	public static function hasSameCategoryName($categoryName,$alias,$parentId,$categoryId){
		if(!$categoryName){
			throw new BusinessException('分类名称不能为空',Base::DATA_EMPTY_NAME);
		}
		
		$eventCategoryDefineFilter = new \haibao\cms\model\filter\EventCategoryDefine();
		if($categoryId){
			$eventCategoryDefineFilter->where($eventCategoryDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $categoryId);
		}
		$eventCategoryDefineFilter->where($eventCategoryDefineFilter::CONDITION_FIELD_NAME_PARENTID, Condition::CONDITION_EQUAL, $parentId);
		if(!$alias){
			$eventCategoryDefineFilter->where($eventCategoryDefineFilter::CONDITION_FIELD_NAME_CATEGORY_NAME, Condition::CONDITION_EQUAL, $categoryName);
		}else{
			$eventCategoryDefineFilter->where($eventCategoryDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,array(
					new Condition($eventCategoryDefineFilter::CONDITION_FIELD_NAME_CATEGORY_NAME, Condition::CONDITION_EQUAL, $categoryName),
					new Condition($eventCategoryDefineFilter::CONDITIOIN_FIELD_NAME_CATEGORY_ALIAS, Condition::CONDITION_LIKE, '%'.$alias.'%'),
					new Condition($eventCategoryDefineFilter::CONDITION_FIELD_NAME_CATEGORY_NAME, Condition::CONDITION_LIKE, '%'.$alias.'%')
			));
		}
		$data = new CategoryDefineData();
		if($data->getAll($eventCategoryDefineFilter)){
			return false;
		}else{
			return true;
		}
	}
	
	public static function getCategoryByEventId($eventId){
		if(!$eventId){
			return Null;
		}
		$data = new CategoryDefineData();
		return $data->getCategoryByEventId($eventId);
	}
	
}